package com.jfs.qy.chain.component;

import com.alibaba.fastjson2.JSON;
import com.jfs.qy.chain.AbstractServerStartHandler;
import com.jfs.qy.config.component.AlarmTask;
import com.jfs.qy.config.component.TaskExecutorReactor;
import com.jfs.qy.entity.request.SearchAlarmEventRequest;
import com.jfs.qy.entity.response.SearchAlarmEventResponse;
import com.jfs.qy.enums.NvrApiEnum;
import com.jfs.qy.service.AlarmEventService;
import com.jfs.qy.service.LogoutService;
import com.jfs.qy.session.NvrSessionStorage;
import com.jfs.qy.strategy.AlarmEventProcessStrategy;
import com.jfs.qy.utils.SystemCommonUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Lazy;
import org.springframework.http.*;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.net.URISyntaxException;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.util.List;

/**
 * @Description 责任链中查询告警事件组件
 * @E-mail cwm3412638@163.com
 * @Author cwm
 * @Date 2022/11/4
 */
@Component(AlarmEventComponent.BEAN_NAME)
@Slf4j
public class AlarmEventComponent extends AbstractServerStartHandler implements AlarmEventService {
    public final static String BEAN_NAME = "alarmEventComponent";
    private final RestTemplate restTemplate;

    private final TaskExecutorReactor taskExecutorReactor;
    private final SystemCommonUtils systemCommonUtils;
    private final LoginComponent loginComponent;
    /**
     * 下一个责任链对象
     *
     * @param abstractServerStartHandler
     */
    private final AbstractServerStartHandler abstractServerStartHandler;
    private final List<AlarmEventProcessStrategy> alarmEventProcessStrategyList;

    public AlarmEventComponent(RestTemplate restTemplate,
                               TaskExecutorReactor taskExecutorReactor,
                               SystemCommonUtils systemCommonUtils,
                               @Qualifier(value = ProcessAlarmEventComponent.BEAN_NAME)
                               AbstractServerStartHandler abstractServerStartHandler,
                               List<AlarmEventProcessStrategy> alarmEventProcessStrategyList,
                               @Lazy  LoginComponent loginComponent) {
        this.restTemplate = restTemplate;
        this.taskExecutorReactor = taskExecutorReactor;
        this.systemCommonUtils = systemCommonUtils;
        this.abstractServerStartHandler = abstractServerStartHandler;
        this.alarmEventProcessStrategyList = alarmEventProcessStrategyList;
        this.loginComponent=loginComponent;
    }

    @Override
    public void handlerOperation() throws URISyntaxException, NoSuchAlgorithmException, KeyStoreException, KeyManagementException {
        //退出登录接口实现类
        LogoutService logoutService = () -> {
            final HttpEntity<?> entity = new HttpEntity<>(null, NvrSessionStorage.headers);
            String requestUrl = systemCommonUtils.getRequestUrl(NvrApiEnum.LOGOUT);
            return restTemplate.exchange(requestUrl, HttpMethod.POST, entity, String.class);
        };
        //创建查询告警事件
        AlarmTask alarmTask = new AlarmTask(this, alarmEventProcessStrategyList,logoutService,loginComponent);
        taskExecutorReactor.schedule(alarmTask, 0, systemCommonUtils.getAlarmInterval());
        //启用下一个责任链
        abstractServerStartHandler.handlerOperation();
    }

    @Override
    public ResponseEntity<SearchAlarmEventResponse> searchAlarmEvent(SearchAlarmEventRequest searchAlarmEventRequest) {
        //拼接请求url
        String requestUrl = systemCommonUtils.getRequestUrl(NvrApiEnum.EVENT_CHECK);
        HttpHeaders headers = NvrSessionStorage.headers;
        headers.set(HttpHeaders.ACCEPT, MediaType.ALL_VALUE);
        headers.set(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_UTF8_VALUE);
        final HttpEntity<?> entity  = new HttpEntity<>(JSON.toJSONString(searchAlarmEventRequest), headers);
        return restTemplate.exchange(requestUrl, HttpMethod.POST, entity, SearchAlarmEventResponse.class);
    }
}
